function bciTopoplotResults(bci,W)
% PURPOSE: plot CSP or weights
%
% INPUT:
%
% OUTPUT:
%

if isfield(bci,'chanLocFile') && ~isempty(bci.chanLocFile),
    if isempty(strfind(bci.chanLocFile,'.grid')),
        [chNum,X,Y,Width,Height,Lbl,Rem] = textread(bci.chanLocFile,'%f %f %f %f %f %q %q');
    else
        [X,Y,Lbl]=bciReadGridLayout(bci.chanLocFile);
        Lbl={int2str(Lbl)};
        chNum = transpose(1:length(X));
    end
    if length(chNum)~=length(bci.init.dataChan),
        bci.init.dataChan = chNum';
    end
    useLayout=true;
else
    chNum=bci.init.dataChan;
    useLayout=false;
end

if bci.param.CSPfilter>0, % Prepare common spatial patterns 
    filtID=[1:bci.param.CSPfilter,size(bci.cspW,2)-bci.param.CSPfilter+1:size(bci.cspW,2)];
    if isfield(bci,'cspOvsOmask'),
        meanCSPW=mean(bci.cspW(:,:,:,2),3); % mean throuh freqbands for 2nd classPair (1vs3) 
    else
        meanCSPW=mean(bci.cspW,3); % mean throuh freqbands
    end
    A=abs(inv(meanCSPW)');

    curW=zeros(size(A,1),length(filtID));
    for k=1:length(filtID),
        curW(:,k)=A(:,filtID(k))-mean(A(:,filtID(k)));
        subTitle{k} = sprintf('CSP Pattern %i', k);
    end
    Wtmp=zeros(length(chNum),length(filtID));
    chIdx = bciFind(bci.init.dataChan,bci.preprocChan(bci.goodChan));
    Wtmp(chIdx,:)=curW;
   
    unit='';
    nx=ceil(sqrt(size(Wtmp,2)+1));
    ny=nx;
else % prepare SVM-weights
    Wtmp=zeros(length(bci.init.dataChan),length(bci.selectedFeat));
    %[dummy,chIdx,dummy]=intersect(bci.init.dataChan,bci.preprocChan(bci.goodChan));
    chIdx=1:size(W,1); % channel mapping moved to bciMultiCompareFeatures
    %chIdx = bciFind(bci.init.dataChan,bci.preprocChan(bci.goodChan));
    Wtmp(chIdx,:)=W; % 
    
    if bci.param.specMethod==3,
        subTitle = cellstr(int2str([bci.selectedFeat]'));
        unit='. AR Coeff';
    elseif bci.param.specMethod>7,
        subTitle = cellstr(int2str([bci.selectedFeat]'));
        unit='. Feature';        
    else
        subTitle = cellstr(num2str(bci.param.freq(bci.selectedFeat)','%3.2f'));
        unit=' Hz';
    end
    cfg.maplimits=[-max(abs(Wtmp(:))) max(abs(Wtmp(:)))];
    nx=ceil(sqrt(size(Wtmp,2)+1));
    ny=nx;
end

%% PLOTS 
%weights on a grid. Colormap is common to all grid plots
for k=1:size(Wtmp,2),
    subplot(ny,nx,k);
    maxW=max(abs(Wtmp(:)));
    minW=-max(abs(Wtmp(:)));
    if useLayout,
        %cfg.layout=bci.chanLocFile;
        if isempty(strfind(bci.chanLocFile,'.grid')),
            topoplot(struct,X,Y,Wtmp(:,k),Lbl); % head plot
        else
            bciGridPlot(struct,X,Y,Wtmp(:,k),Lbl);
        end
    else
        bciTopoplotGrid(Wtmp(:,k));
    end
    set(gca,'clim',[minW maxW]);
    title(['topoplot: ' subTitle{k} unit]);
end
% Matrix of weights
if nargin>1 && bci.param.specMethod<8,
    if k<nx*ny,
        subplot(nx,ny,k+1);
    else
        figure;
    end
    % plot the feature map
    imagesc(W');
    h=colorbar;
    set(get(h,'Ylabel'),'String','Feature weights')
    if bci.param.specMethod>0,
        yTL=bci.param.freq(bci.selectedFeat);
    else
        if bci.param.CSPfilter>0,
            yTL=1:length(filtID);
        else
            yTL=linspace(0,bci.init.nFeatPerChan/bci.srate,bci.init.nFeatPerChan);
        end
    end
    set(gca,'ytick',1:length(yTL),'yticklabel',yTL);
    set(gca,'xtick',1:length(bci.goodChan),'xticklabel',bci.preprocChan(bci.goodChan));
    ylabel('Frequency [Hz]');
    xlabel('Channel number');
end